package com.badibadi.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.abcpinying.CharacterParser;
import com.badibadi.abcpinying.PinyinComparator;
import com.badibadi.abcpinying.SeeOtherPeopleAdapter;
import com.badibadi.abcpinying.SortModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ClubMemberActivity extends BaseActivity implements
		IXListViewListener {

	private XListView mListView;
	private SeeOtherPeopleAdapter adapter;
	private String cid = null, cj_uid = null;
	private int Page;
	private Results results;
	private List<SortModel> SourceDateList;
	private List<SortModel> List;
	private CharacterParser characterParser;
	private PinyinComparator pinyinComparator;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ClubMemberActivity.this);
				try {
					Utils.showMessage(ClubMemberActivity.this, getResources().getString(R.string.l_net_error));
					onLoad();
				} catch (Exception e) {
					// TODO: handle exception
				}
			
				break;
			case 2:
				try {
					Utils.ExitPrgress(ClubMemberActivity.this);
					Updata();
					onLoad();
				} catch (Exception e) {
					// TODO: handle exception
				}
		
				break;
			case 3:
				try {
					Utils.ExitPrgress(ClubMemberActivity.this);
					Utils.showMessage(ClubMemberActivity.this, getResources().getString(R.string.l_xa10));
					onLoad();
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.club_member);
		init();
	}

	private void init() {
		try {
			cid = getIntent().getStringExtra("cid");
			cj_uid = getIntent().getStringExtra("cj_uid");
		} catch (Exception e) {
			   
		}
		Page = 0;
		cid = "11";
		cj_uid = "1";
		SourceDateList = new ArrayList<SortModel>();
		characterParser = CharacterParser.getInstance();
		pinyinComparator = new PinyinComparator();
		mListView = (XListView) this.findViewById(R.id.club_Member_xlistView);
		this.findViewById(R.id.return_btn).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						ClubMemberActivity.this.finish();
					}
				});
		initData();
		Collections.sort(SourceDateList, pinyinComparator);
		adapter = new SeeOtherPeopleAdapter(ClubMemberActivity.this,
				SourceDateList, cj_uid);
		mListView.setPullLoadEnable(true);
		mListView.setAdapter(adapter);
		mListView.setXListViewListener(this);
		mListView.startLoadMore();

	}

	private void menber(final int page) {
		// Utils.showPrgress(ClubMemberActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				List = new ArrayList<SortModel>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("cid", cid);
				map.put("uid", Utils.getUid(ClubMemberActivity.this));
				map.put("type", "2");// 2:俱乐部会员总览列表 1：俱乐部转让
				map.put("page", page);
				map.put("pageNum", "10");
//				   ("查看俱乐部会员传送的数据是:" + map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/menber");
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
//					   ("俱乐部会员返回数据是:" + content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						try {
							List = JSONUtils.getListByJsonString(
									results.getRetmsg(), SortModel.class);
							mHandler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						mHandler.sendEmptyMessage(3);
					}
				} else {
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	protected void Updata() {
		//    ("执行更新数据");
		SourceDateList.addAll(filledData(List));
		// for (int i = 0; i < SourceDateList.size(); i++) {
		//    ("数据是:" + SourceDateList.get(i).toString());
		// }
		Collections.sort(SourceDateList, pinyinComparator);
		adapter.notifyDataSetChanged();
	}

	protected void initData() {
		// 更新了数据
		// SourceDateList = filledData(List);
		SourceDateList.addAll(filledData(List));
	}

	/**
	 * 为ListView填充数据
	 * 
	 * @param date
	 * @return
	 */
	protected List<SortModel> filledData(List<SortModel> date) {
		List<SortModel> mSortList = new ArrayList<SortModel>();
		if (date != null && !date.isEmpty()) {
//			   ("不为空");
			for (int i = 0; i < date.size(); i++) {
				SortModel sortModel = new SortModel();
				sortModel.setNickName(date.get(i).getNickName());
				sortModel.setHead(date.get(i).getHead());
				sortModel.setId(date.get(i).getId());
				sortModel.setMotto(date.get(i).getMotto());
				sortModel.setUid(date.get(i).getUid());
				sortModel.setIs_firend(date.get(i).getIs_firend());
				// 汉字转换成拼音
				String pinyin = characterParser.getSelling(""
						+ date.get(i).getNickName());
				String sortString = pinyin.substring(0, 1).toUpperCase();
				// 正则表达式，判断首字母是否是英文字母
				if (sortString.matches("[A-Z]")) {
					sortModel.setSortLetters(sortString.toUpperCase());
				} else {
					sortModel.setSortLetters("#");
				}
				sortModel.setMotto("这只是一项测试");

				mSortList.add(sortModel);
			}
		} else {
//			   ("为空");
		}
		return mSortList;
	}

	protected void onLoad() {
		// TODO Auto-generated method stub
		mListView.stopRefresh();
		mListView.stopLoadMore();
		mListView.setRefreshTime(""+getResources().getString(R.string.ganggang));
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Page = 1;
		SourceDateList.clear();
		menber(Page);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		menber(++Page);
	}

}
